$.HOMESTICK = new Object();
$.HOMESTICK.active = true;
$.HOMESTICK.elements = $('#subSectionStick .subSection').toArray();
$.HOMESTICK.elementsCount = $('#subSectionStick .subSection').size();
$.HOMESTICK.firstElement = $('#subSectionStick .subSection:first');
$.HOMESTICK.openFirstElement = $.HOMESTICK.firstElement;
$.HOMESTICK.defaultRotation = 45;
$.HOMESTICK.startRotation = 360;
$.HOMESTICK.currentRotation = 0;
$.HOMESTICK.maxCount = 8;
$.HOMESTICK.openMenuRotation = 0;
$.HOMESTICK.LastMenuRotation = 0;

if ($('#subSectionStick .subSection').size() > $.HOMESTICK.maxCount) {
    $.HOMESTICK.defaultRotation = 360 / $('#subSectionStick .subSection').size();
}

$.HOMESTICK.init = function () {

    $('#subSectionStick').css({display: 'block'});
    $.HOMESTICK.openMenu();

    $('#subSectionStick .subSection').on("mouseover", function () {
    });
    $('#subSectionStick .subSection').on("mouseleave", function () {
    });
}

$.HOMESTICK.openMenuVal = function () {
    return (100 * Math.random());//(361*Math.random());
};

$.HOMESTICK.openMenu = function () {

    $.HOMESTICK.currentRotation = 0;
    if (!$('body').hasClass('explorer')) {
        $.HOMESTICK.openMenuRotation = $.HOMESTICK.openMenuVal();
        TweenLite.to($('#subSectionStick'), 3, {css: {rotation: $.HOMESTICK.openMenuRotation}, ease: Expo.easeOut, onComplete: function () {
            $.HOMESTICK.LastMenuRotation = $.HOMESTICK.openMenuRotation;
            $.SLIDES.goShortcut();
        }});
        $.HOMESTICK.openStick(0);
    } else {
        $.HOMESTICK.openStickIE(0);
    }

}

$.HOMESTICK.openStick = function (elementIndex) {
    //if(elementIndex >= $('#subSectionStick .subSection').size()){return true;}else{console.log('count:'+$.HOMESTICK.maxCount + ' elentiIndex:'+elementIndex );}
    $($.HOMESTICK.elements[elementIndex]).css({transform: 'rotate(' + $.HOMESTICK.currentRotation + 'deg)'});
    $($.HOMESTICK.elements[elementIndex]).find('.btn').css({transform: 'rotate(-' + parseInt($.HOMESTICK.openMenuRotation + $.HOMESTICK.currentRotation) + 'deg)'});
    $.HOMESTICK.currentRotation += $.HOMESTICK.defaultRotation;
    $.HOMESTICK.showStick(elementIndex);

    $.MENUNAV.transitions = true;


    UTILS.getUserAgent();
    if (UTILS.isDevice) {
        $($.HOMESTICK.elements[elementIndex]).find('.btn').each(function () {

            $.HOMESTICK.hoverize($(this));

        });
    }
}


$.HOMESTICK.openStickIE = function (elementIndex) {

    $($.HOMESTICK.elements[elementIndex]).css({'display': 'none !important'});
    $($.HOMESTICK.elements[elementIndex]).find('.btn').css({});
    //$.HOMESTICK.showStick(elementIndex);
    $('.explorer .subSectionStick').hide();


    $.MENUNAV.transitions = true;

}


$.HOMESTICK.showStick = function (elementIndex) {
    if (elementIndex >= $('#subSectionStick .subSection').size()) {
        return true;
    }
    TweenLite.to($($.HOMESTICK.elements[elementIndex]), 3, {css: {opacity: 1}, ease: Expo.easeOut});
    TweenLite.to($($.HOMESTICK.elements[elementIndex]).find('.btn'), 3, {css: {marginTop: '20px'}, ease: Elastic.easeOut});
    if (elementIndex < $('#subSectionStick .subSection').size()) {
        $.HOMESTICK.openStick(elementIndex + 1);
    }

    var dataRotation = parseInt($($.HOMESTICK.elements[elementIndex]).find('.btnMask').attr('data-rotation'));
    TweenLite.to($($.HOMESTICK.elements[elementIndex]).find('.btnMask'), 0, {css: {rotation: dataRotation}});

    $($.HOMESTICK.elements[elementIndex]).find('.btn').hover(function () {
        $.HOMESTICK.hoverize($(this));
    }, function () {
        $.HOMESTICK.unHoverize($(this));
    });
    $($.HOMESTICK.elements[elementIndex]).find('.btn').click(function () {
        if ($('body').hasClass('home')) {
            var temDataMode = $(this).attr('data-mode');
            var temDataUrl = $(this).attr('data-url');
            var dataObject = $(this).data();
            if (temDataMode == 'player') {
                $.FULLTOP.loadUrl(temDataUrl, temDataMode, dataObject);
                console.log('call from  stick for player');
            }
            else if (temDataMode == 'url') {
                $.FULLTOP.loadUrl(temDataUrl, temDataMode, dataObject);
                console.log('call from  stick for url');
            }
            else {
                $.HOMESTICK.closeSticks(temDataUrl);
                console.log('call from  stick for menu');
            }
        }
    });

    if ($('body').hasClass('explorer')) {
        $('.explorer .subSectionStick, .explorer .btn').hide();
    }
}

$.HOMESTICK.hoverize = function (target) {
    TweenLite.to(target.find('.btnBg'), .8, {css: {opacity: 1}, ease: Expo.easeOut});
    var dataRotation = parseInt(target.find('.btnMask').attr('data-rotation'));
    var dataRotation_end = parseInt(target.find('.btnMask').attr('data-rotation-end'));
    TweenLite.to(target.find('.btnMask'), .3, {css: {rotation: dataRotation_end}, ease: Expo.easeOut, onComplete: function () {
        TweenLite.to(target.find('.btnContent'), .3, {css: {opacity: 1}, ease: Expo.easeOut});
    }});
    TweenLite.to(target, .3, {css: {scaleX: 1.1, scaleY: 1.1}, ease: Elastic.easeOut});
    TweenLite.to(target.find('.btnCircle'), 1, {css: {scaleX: 1.2, scaleY: 1.2, opacity: 1}, ease: Elastic.easeOut});

    var dataID = parseInt(target.parent().attr('data-id'));
    for (var i = (dataID - 1); i >= 0; i--) {
        //minimizza a sinistra
        var distanza = 1 - (Math.abs(dataID - i) / 10);
        TweenLite.to($($.HOMESTICK.elements[i]), .8, {css: {scaleX: distanza, scaleY: distanza}, ease: Elastic.easeOut});
    }
    for (var i = (dataID + 1); i < $.HOMESTICK.elementsCount; i++) {
        //minimizza a destra
        var distanza = 1 - (Math.abs(dataID - i) / 10);
        TweenLite.to($($.HOMESTICK.elements[i]), .8, {css: {scaleX: distanza, scaleY: distanza}, ease: Elastic.easeOut});
    }
}

$.HOMESTICK.unHoverize = function (target) {
    TweenLite.to($(target).find('.btnBg'), .3, {css: {opacity: 0}, ease: Expo.easeOut});
    var dataRotation = parseInt(target.find('.btnMask').attr('data-rotation'));
    TweenLite.to($(target).find('.btnMask'), .2, {css: {rotation: dataRotation}, ease: Expo.easeOut});
    TweenLite.to($('#subSectionStick .subSection'), .8, {css: {scaleX: 1, scaleY: 1}, ease: Elastic.easeOut});
    TweenLite.to($('#subSectionStick .subSection .btnContent'), .2, {css: {opacity: 0}, ease: Expo.easeOut});

    TweenLite.to(target.find('.btnCircle'), .3, {css: {scaleX: 1, scaleY: 1, opacity: 0}, ease: Elastic.easeOut});
}

$.HOMESTICK.loadPage = function ($dataUrlLoad) {
    $.MENUNAV.loadContent($dataUrlLoad);
}

$.HOMESTICK.openSticks = function () {
    $('#subSectionStick, #atari, #atariArea').show();
    //TweenLite.to($('#subSectionStick, #atari, #atariArea'), 1, {css:{rotation:"+=180", opacity:1,scaleX:1,scaleY:1}, ease:Expo.easeOut, onComplete:function(){}});
}
$.HOMESTICK.closeSticks = function ($dataUrlLoad) {
    if (!($dataUrlLoad == undefined || $dataUrlLoad == 'undefined' || $dataUrlLoad == null)) {
        $.HOMESTICK.loadPage($dataUrlLoad);
    }

    /*TweenLite.to($('#subSectionStick, #atari, #atariArea'), 1, {css:{rotation:"-=180", opacity:0,scaleX:.5,scaleY:.5}, ease:Expo.easeOut,onComplete:function(){
     $('#subSectionStick, #atari, #atariArea').hide();
     }});*/
}
